<?php
	/**
	 * 登录
	 */
    class Bind extends APIBase {
		
		public $tag = "Bind";
		//public $isLogin = false;
		
		public function before() {
			$this->initMysql();
			$this->initCacheRedis();
			return true;
		}
	
		public function logic() {
 
			if (!isset($this->param['password'])) {
				
				$this->returnError(311, "请输入密码");
			}
			
			
				
			if (!isset($this->param['puid']) && $this->param['puid']=='') {
				$this->returnError(311, "机器码不能为空");
			 } 
			 
			 $flag = $this->param['flag'];
		  
			$this->initDataRedis($this->uid);	
			$row = $this->mysql->select("player", "*", array('id' => $this->uid));
			
			if (count($row) == 0) {
				$this->returnError(312, '用户不存在！');
			} 

			$password = md5($this->param['password'].$row[0]['bank_salt']);
			if ($password != $row[0]['bank_password']) {
				$this->returnError(312, '密码错误！');
			}
			if ($row[0]['status'] == 0){
				$this->returnError(310,"用户不存在！");
			}
			
			 
			if($flag){
			  $result = $this->mysql->update('player',array('bindpuid'=>$this->param['puid']),array('id'=>(int)$this->uid));
			  $this->data_redis->hSet("hu:$this->uid", 'bindpuid',$this->param['puid']);
			
			  if($result){
				$this->returnData($result,0, "绑定成功！");
			  }else{
				$this->returnError(310,"绑定失败！"); 	
			  }
			  
			}else{
			   
			  $result = $this->mysql->update('player',array('bindpuid'=>''),array('id'=>(int)$this->uid));
			  $this->data_redis->hSet("hu:$this->uid", 'bindpuid','');
			  if($result){
				$this->returnData($result,0, "解除绑定成功！");
			  }else{
				$this->returnError(310,"解除绑定失败！"); 	
			  }	
				
			}
			
			
			
			
			
		}
    	public function after() {
    		$this->deinitDataRedis();
			$this->deinitCacheRedis();
			$this->deinitMysql();
			
		}
    }
?>
